blog

Home / DeveloperSection / Blogs / Explain the concept and use DOM Manipulation

Explain the concept and use DOM Manipulation

Explain the concept and use DOM Manipulation

Ravi Vishwakarma 215 18-Jun-2024

DOM (Document Object Model) manipulation in JavaScript is the process of dynamically changing a web page's structure, content, and style. The DOM represents an HTML or XML document as a tree structure, where each node corresponds to a part of the document (such as an element, attribute, or piece of text). Using JavaScript, you can interact with this tree to modify the document's content and appearance.

Key Concepts and Use Cases of DOM Manipulation

  1. Selecting Elements: To manipulate elements, you first need to select them from the DOM.
  2. Modifying Elements: Change the content, attributes, styles, or structure of elements.
  3. Creating and Inserting Elements: Dynamically add new elements to the DOM.
  4. Removing Elements: Remove existing elements from the DOM.
  5. Event Handling: Respond to user actions like clicks, input, or form submissions.

 

Selecting Elements

You can use several methods to select elements from the DOM:

document.getElementById(id): Selects an element by its ID.
document.getElementsByClassName(className): Select elements by their class name.
document.getElementsByTagName(tagName): Select elements by their tag name.
document.querySelector(selector): Selects the first element that matches a CSS selector.
document.querySelectorAll(selector): Selects all elements that match a CSS selector.

// Select element by ID
const header = document.getElementById('header');
// Select elements by class name
const items = document.getElementsByClassName('item');
// Select elements by tag name
const paragraphs = document.getElementsByTagName('p');
// Select the first element that matches a CSS selector
const firstItem = document.querySelector('.item');
// Select all elements that match a CSS selector
const allItems = document.querySelectorAll('.item');

Modifying Elements

You can modify elements' content, attributes, and styles:

element.textContent or element.innerHTML: Change the text content or HTML of an element.
element.setAttribute(name, value): Set an attribute's value.
element.style.property: Change an element's style.

const header = document.getElementById('header');
// Change text content
header.textContent = 'New Header Text';
// Change HTML content
header.innerHTML = '<span>New Header HTML</span>';
// Set an attribute
header.setAttribute('class', 'new-class');
// Change style
header.style.color = 'blue';
header.style.fontSize = '24px';

Creating and Inserting Elements

You can create new elements and insert them into the DOM:

document.createElement(tagName): Create a new element.
parentNode.appendChild(node): Append a child node to a parent node.
parentNode.insertBefore(newNode, referenceNode): Insert a new node before a reference node.

// Create a new paragraph element
const newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
// Append the new paragraph to the body
document.body.appendChild(newParagraph);
// Insert a new item before an existing item
const newItem = document.createElement('li');
newItem.textContent = 'New Item';
const list = document.querySelector('ul');
const firstItem = list.querySelector('li');
list.insertBefore(newItem, firstItem);

Removing Elements

You can remove elements from the DOM:

parentNode.removeChild(node): Remove a child node from a parent node.
element.remove(): Directly remove an element.

const itemToRemove = document.querySelector('.item');
// Remove using parent node
itemToRemove.parentNode.removeChild(itemToRemove);
// Or remove directly
itemToRemove.remove();

Event Handling

You can add event listeners to elements to respond to user interactions:

element.addEventListener(event, function): Add an event listener.

const button = document.querySelector('button');
// Add a click event listener
button.addEventListener('click', function() {
   alert('Button was clicked!');
});

 


Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By